package io.github.junxworks.qt.modules.data.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import io.github.junxworks.ep.core.orm.BaseMapper;
import io.github.junxworks.qt.config.ds.Sharding;
import io.github.junxworks.qt.modules.data.dto.StockStatDto;
import io.github.junxworks.qt.modules.data.entity.DStockMv;

@Sharding
public interface StockStatMapper extends BaseMapper {

	@Delete("delete from d_stock_change_stat where ts_code=#{tsCode} and trade_date=#{date}")
	int deleteStockChangeStat(@Param("tsCode") String tsCode, @Param("date") String date);

	@Delete("delete from d_stock_change_stat where trade_date=#{date}")
	int deleteStockChangeStatByDate(@Param("date") String date);

	@Select("select close `high`,close `low` from d_stock_trade_daily where ts_code=#{tsCode} and trade_date=#{date}")
	StockStatDto queryStateChangeByDate(@Param("tsCode") String tsCode, @Param("date") String date);

	@Select("select max(close) `high`,min(close) `low` from d_stock_trade_daily where ts_code=#{tsCode} and trade_date>=#{bDate} and trade_date<=#{eDate}")
	StockStatDto queryStateChangeByDateRange(@Param("tsCode") String tsCode, @Param("bDate") String bDate, @Param("eDate") String eDate);
	
	@Delete("delete from d_stock_mv where trade_mon=#{mon}")
	int deleteTotalMv(@Param("mon") String mon);

	@Select("""
				SELECT t.ts_code,substring(t.trade_date,1,6) `trade_mon`, t.total_mv,#{endDate} `end_date`,t.pe_ttm,t.pe,t.pb,t.dv_ttm,t.ps_ttm,t.close
				FROM d_stock_trade_daily_basic t,
				     (SELECT ts_code, max(trade_date) trade_date
				      FROM d_stock_trade_daily_basic
				      WHERE trade_date <=#{endDate} AND trade_date >= #{startDate}
				      GROUP BY ts_code) a
				WHERE t.trade_date = a.trade_date AND t.ts_code = a.ts_code
			""")
	List<DStockMv> generateTotalMv(@Param("startDate") String startDate, @Param("endDate") String endDate);
}
